/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package solicitudesfiec.autenticacion;

import solicitudesfiec.autenticacion.bean.DatosUsuario;
import solicitudesfiec.autenticacion.bean.NewDataSet;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;

/**
 *
 * @author Larry
 */
public class CargadorXml {

    public NewDataSet getNewDataSet(String xml) {
        XStream xstream = new XStream(new DomDriver());
        xstream.alias("NewDataSet", NewDataSet.class);
        xstream.aliasAttribute(NewDataSet.class, "datosUsuario", "DATOS_USUARIO");
        xstream.aliasAttribute(DatosUsuario.class, "matricula", "MATRICULA");
        xstream.aliasAttribute(DatosUsuario.class, "cedula", "CEDULA");
        xstream.aliasAttribute(DatosUsuario.class, "apellidos", "APELLIDOS");
        xstream.aliasAttribute(DatosUsuario.class, "nombres", "NOMBRES");
        xstream.aliasAttribute(DatosUsuario.class, "nombreCompleto", "NOMBRE_COMPLETO");
        xstream.aliasAttribute(DatosUsuario.class, "unidad", "UNIDAD");
        xstream.aliasAttribute(DatosUsuario.class, "rol", "ROL");
        xstream.aliasAttribute(DatosUsuario.class, "correo", "CORREO");
        xstream.aliasAttribute(DatosUsuario.class, "direccion", "DIRECCION");
        xstream.aliasAttribute(DatosUsuario.class, "telefono", "TELEFONO");
        xstream.aliasAttribute(DatosUsuario.class, "celular", "CELULAR");
        xstream.aliasAttribute(DatosUsuario.class, "sexo", "SEXO");
        xstream.aliasAttribute(DatosUsuario.class, "carrera", "CARRERA");
        xstream.aliasAttribute(DatosUsuario.class, "especializacion", "ESPECIALIZACION");
        xstream.aliasAttribute(DatosUsuario.class, "fechaNacimiento", "FECHANACIMIENTO");
        xstream.aliasAttribute(DatosUsuario.class, "promedio", "PROMEDIO");
        xstream.aliasAttribute(DatosUsuario.class, "materiasAprobadas", "MATERIASAPROBADAS");
        xstream.aliasAttribute(DatosUsuario.class, "lastChanged", "LASTCHANGED");

//	InputStream is = new BufferedInputStream(new FileInputStream("Prueba.xml"));
        NewDataSet obj = (NewDataSet) xstream.fromXML(xml);
        return obj;
    }

    public NewDataSet getNewDataSet(Node elemento) throws TransformerException{
        String xml = nodeToString(elemento);
        return getNewDataSet(xml);
    }
    public static String nodeToString(Node ele) throws TransformerException {
// Set up the output transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

// Print the DOM node
        StringWriter sw = new StringWriter();
        StreamResult result2 = new StreamResult(sw);
        DOMSource source = new DOMSource(ele);
        trans.transform(source, result2);
        String xmlString = sw.toString();
        return xmlString;
    }
}
